#!/bin/bash
gra_file=${1}
echo $gra_file
gdl_file=${gra_file%%.*}
gdl_file=$gdl_file".gdl"
echo $gdl_file

if [ -e $gdl_file ] ; then
    rm $gdl_file
fi

touch $gdl_file

echo "graph:{title: \"Test\"
        layoutalgorithm: tree   // Yes, this is actually not a tree.
        scaling        : 2.0
        colorentry 42  : 152 222 255
        node.shape     : circle   //=================================
        node.color     : 42        //   aiSee -- Graph Visualization  
        node.height    : 32        //       http://www.aisee.com      
        node.fontname  : \"helvB08\" // Copyright 2000 - 2002     
        edge.color     : darkred   //AbsInt Angewandte Informatik GmbH
        edge.arrowsize :  6        //         aisee@absint.com        
        node.textcolor : darkblue  // Courtesy I. Darwin & G. Collyer 
        splines        : yes       //=================================

" >> $gdl_file

source=""
target=""
node=""

## write node
while read line; do
    if [ ${line##* } = "#" ]; then
	#echo "yes"
	node=${line%%:*}
	echo "node: {title: \"$node\"}" >> $gdl_file
    fi
done <$gra_file


echo "" >> $gdl_file

##write edges
while read line; do
    if [ ${line##* } = "#" ]; then
	#echo "yes"                                                                                          
        source=${line%%:*}
        target=${line#*:}
        target=${target%%\#}
        #echo "tar:"$target                                                                                  
        if [ ${#target} -ne 0 ]; then
            IFS=" "
            for tar in $target
            do
                echo "edge: {source: \"$source\" target: \"$tar\"}" >> $gdl_file                                                                                    
            done
        fi
        #echo $source"-->"$target                                                                            
    fi
done < $gra_file

echo "}" >> $gdl_file






